Pub Sub
6.9.0The Pub/Sub API enables applications to:
- publish messages on a specific topic;
- subscribe for messages on a specific topic;
When an application publishes a message on a specific topic, the Pub/Sub API delivers it to other applications that have subscribed to that topic.
The "raw Pub/Sub" support allows io.Connect Desktop to work with applications already using a Pub/Sub technology. Before writing new Pub/Sub based code, please consider the higher level Interop services provided by io.Connect Destop: Request/Response, Streaming, Discovery and Shared Contexts. Utilizing these services, instead of creating them from scratch, can save you time and also provide you with a more robust service that can interact with applications by different dev teams and vendors.
The Pub/Sub API is accessible through the io.bus
object.
APIobject
Methods
publishmethod
Signature
(topic: string, data: object, options?: MessageOptions) => void
Description
Publishes the provided data on a specific topic. An optional object can be provided to publish a message to specific peers.
Parameters
Name | Type | Required | Description |
---|---|---|---|
topic | string | Topic on which to publish the message. |
|
data | object | Data to publish. |
|
options | MessageOptions | An optional object with message options. |
Example
io.bus.publish("prices", { RIC: "VOD.L", price: 21.2 });
subscribemethod
Signature
(topic: string, callback: (data: object, topic: string, source: Instance) => void, options?: MessageOptions) => Promise<Subscription>
Description
Subscribe for receiving data published on specific topic on the message bus. The provided callback will be invoked for each received message. An optional object can be provided to receive messages published only by specific peers.
Returns a Promise
which resolves if the subscription is successful. The Promise
resolves with a subscription object which can be used to unsubscribe and stop receiving messages.
Parameters
Name | Type | Required | Description |
---|---|---|---|
topic | string | Topic to which to subscribe. |
|
callback | (data: object, topic: string, source: Instance) => void | Function that will handle the received data. |
|
options | MessageOptions | An optional object with message options. |
Example
io.bus.subscribe("prices", function(data, topic, source) {
console.log(data, topic, source);
});
MessageOptionsobject
Description
If routingKey
is provided when publishing a message, the message will be delivered only to peers that have provided the same routing key
or no routing key during subscription.
If routingKey
is provided when subscribing for a topic, only messages that have been published with a matching routing key or no routing key
at all will be received by the subscriber.
If target
is provided when publishing a message, the message will be delivered only to subscribers for which all properties of the target object
match the respective fields on the subscribers identity.
If target
is provided when subscribing for a topic, the provided object will be compared to the identity of the publishers of the messages
and only if they match will the messages be received by the subscriber.
Properties
Property | Type | Default | Required | Description |
---|---|---|---|---|
routingKey | string | Routing key specified by the publisher/subscriber. |
||
target | object | Target specified by the publisher/subscriber. |
Subscriptionobject
Description
Subscription object that can be used to unsubscribe from a topic and stop receiving data.
Properties
Property | Type | Default | Required | Description |
---|---|---|---|---|
unsubscribe | () => Promise<void> |